////
//
/// \file ./include/inc/parser/operation_node.hpp
//
//  Copyright 2007, 2008 Chandler Carruth
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations
//  under the License.
//
////

#ifndef inc_parser_operation_node_header
#define inc_parser_operation_node_header

#include <string>

#include <boost/scoped_ptr.hpp>

#include <inc/parser/parse_tree_node.hpp>
#include <inc/parser/ws_parser.hpp>

namespace inc {
namespace parser {

class ExpressionNode;
class LabelNode;
class SymbolNode;

/// \brief  Operation Node
/// \author Chandler Carruth
/// \date   2007.06.2
///
/// This parses operations in INC. These are infix binary operations as well as
/// pre- and postfix unary operations. They are syntactical sugar coating for
/// function calls, and obey all the same semantic rules making them "pure" in
/// the mathematical sense. They take their scope from one of their operands
/// rather than an explicit scope selection, and their operands are general
/// expressions. All operations are also explicitly grouped to eliminate
/// precedence, associativity, and commutativity rules. Note that associativity
/// and commutativity are being considered as options for operations, but there
/// is a strong theory preventing the language from providing precedence
/// rules.

class OperationNode : public ParseTreeNode
{
public:
  OperationNode();
  virtual ~OperationNode();

  virtual bool parse( const iterator& first,
                      const iterator& last,
                      iterator& last_parsed );

  virtual LabelNode* get_lhs_label() const;
  virtual LabelNode* get_rhs_label() const;
  virtual bool is_double_label() const { return get_lhs_label() != NULL; }

  virtual ExpressionNode* get_unary_expr() const;
  virtual bool is_unary_prefix() const;
  virtual bool is_unary_postfix() const;
  virtual bool is_unary() const
      { return get_unary_expr() != NULL || is_double_label(); }

  virtual ExpressionNode* get_lhs_expr() const;
  virtual ExpressionNode* get_rhs_expr() const;
  virtual bool is_binary() const { return !is_unary(); }

  virtual LabelNode* get_op_label() const;
  virtual bool is_op_label() const { return get_op_label() != NULL; }

  virtual SymbolNode* get_op_symbol() const;
  virtual bool is_op_symbol() const { return get_op_symbol() != NULL; }

private:
  /// \name Parsing Helper Routines
  //@{
  bool parse_terminator( const iterator& first,
                         const iterator& last,
                         iterator& last_parsed );
  bool parse_rhs_expr( const iterator& first,
                       const iterator& last,
                       iterator& last_parsed );
  bool parse_prefix_symbol( const iterator& first,
                           const iterator& last,
                           iterator& last_parsed );
  bool parse_prefix_label( const iterator& first,
                          const iterator& last,
                          iterator& last_parsed );
  bool parse_label_label( const iterator& first,
                          const iterator& last,
                          iterator& last_parsed );
  bool parse_postfix_binary_label( const iterator& first,
                                   const iterator& last,
                                   iterator& last_parsed );
  bool parse_postfix_binary_symbol( const iterator& first,
                                    const iterator& last,
                                    iterator& last_parsed );
  bool parse_lhs_expr( const iterator& first,
                   const iterator& last,
                   iterator& last_parsed );
  //@}

  //  Parsing Helpers
  boost::scoped_ptr<WSParser> ws;

  //  Concrete Parse Results
  boost::scoped_ptr<LabelNode> op_label;
  boost::scoped_ptr<SymbolNode> op_symbol;

  boost::scoped_ptr<ExpressionNode> lhs_expr;
  boost::scoped_ptr<ExpressionNode> rhs_expr;

  //  Ambiguous Parse Results
  boost::scoped_ptr<LabelNode> lhs_label;
  boost::scoped_ptr<LabelNode> rhs_label;
};

} //  end parser namespace
} //  end inc namespace
#endif  //  _parser_operation_node_header_
